package com.csu.letaobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.letaobackend.bean.*;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.*;
import com.csu.letaobackend.service.EvaluationService;
import com.csu.letaobackend.vo.ItemVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class EvaluationServiceImpl implements EvaluationService {

      @Autowired
      FavoriteitemDao favoriteitemDao;

      @Autowired
      FavoritestoreDao favoritestoreDao;

      @Autowired
      PictureDao pictureDao;

      @Autowired
      ItemDao itemDao;

      @Autowired
      ShopDao shopDao;

      @Autowired
      ItemevalDao itemevalDao;

      @Override
      public CommonResponse<List<ItemVO>> getCollections(String username) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            List<ItemVO> itemVOList = new ArrayList<>();
            List<Favoriteitem> favoriteitemList = favoriteitemDao.selectList(queryWrapper);
            for (Favoriteitem favoriteitem : favoriteitemList) {
                  Item item = itemDao.selectById(favoriteitem.getItemid());
                  itemVOList.add(entityToVO(item));
            }

            return CommonResponse.createForSuccess(itemVOList);
      }

      @Override
      public CommonResponse<List<ItemVO>> addCollections(String username, int itemid) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            if (itemDao.selectById(itemid) == null)
                  return CommonResponse.createForError("没有该商品");
            queryWrapper.eq("itemid", itemid);
            if (favoriteitemDao.selectOne(queryWrapper) == null) {
                  Favoriteitem favoriteitem = new Favoriteitem();
                  favoriteitem.setItemid(itemid);
                  favoriteitem.setUsername(username);
                  favoriteitemDao.insert(favoriteitem);
                  return  CommonResponse.createForSuccess("收藏成功",getCollections(username).getData());
            } else
                  return CommonResponse.createForError("该商品已被收藏");
      }

      @Override
      public CommonResponse<List<ItemVO>> deleteCollections(String username, int itemid) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            if (itemDao.selectById(itemid) == null)
                  return CommonResponse.createForError("没有该商品");
            queryWrapper.eq("itemid", itemid);
            if (favoriteitemDao.selectOne(queryWrapper) != null) {
                  favoriteitemDao.delete(queryWrapper);
                  return  CommonResponse.createForSuccess("取消收藏成功",getCollections(username).getData());
            } else
                  return CommonResponse.createForError("该商品未被收藏");
      }

      @Override
      public CommonResponse<List<Shop>> getCollectionShops(String username) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            List<Shop> shopList = new ArrayList<>();
            List<Favoritestore> favoritestoreList = favoritestoreDao.selectList(queryWrapper);
            for (Favoritestore favoritestore : favoritestoreList) {
                  Shop shop = shopDao.selectById(favoritestore.getShopid());
                  shopList.add(shop);
            }

            return CommonResponse.createForSuccess(shopList);
      }

      @Override
      public CommonResponse<List<Shop>> addCollectionsShops(String username, int shopid) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            if (shopDao.selectById(shopid) == null)
                  return CommonResponse.createForError("没有该店铺");
            queryWrapper.eq("shopid", shopid);
            if (favoritestoreDao.selectOne(queryWrapper) == null) {
                  Favoritestore favoritestore = new Favoritestore();
                  favoritestore.setUsername(username);
                  favoritestore.setShopid(shopid);
                  favoritestoreDao.insert(favoritestore);
                  return  CommonResponse.createForSuccess("关注成功",getCollectionShops(username).getData());
            } else
                  return CommonResponse.createForError("该店铺已被收藏");
      }

      @Override
      public CommonResponse<List<Shop>> deleteCollectionsShops(String username, int shopid) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            if (shopDao.selectById(shopid) == null)
                  return CommonResponse.createForError("没有该店铺");
            queryWrapper.eq("shopid", shopid);
            if (favoritestoreDao.selectOne(queryWrapper) != null) {
                  favoritestoreDao.delete(queryWrapper);
                  return  CommonResponse.createForSuccess("取消关注成功",getCollectionShops(username).getData());
            } else
                  return CommonResponse.createForError("该店铺未被收藏");
      }


      @Override
      public CommonResponse<List<Itemeval>> addEvaluations(int itemid, String content, double star, String username) {
            Itemeval itemeval = new Itemeval();
            itemeval.setId(getMaxItemevalId() + 1);
            itemeval.setItemid(itemid);
            itemeval.setUsername(username);
            itemeval.setContent(content);
            itemeval.setStar(star);
            itemevalDao.insert(itemeval);
            return CommonResponse.createForSuccess("评论成功",getEvaluations(itemid).getData());
      }

      @Override
      public CommonResponse<List<Itemeval>> getEvaluations(int itemid) {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (itemDao.selectById(itemid) == null)
                  return CommonResponse.createForError("没有该商品");
            queryWrapper.eq("itemid", itemid);
            List<Itemeval> itemevalList = itemevalDao.selectList(queryWrapper);
            return CommonResponse.createForSuccess(itemevalList);
      }

      @Override
      public CommonResponse<IPage<ItemVO>> getCollectionsPages(int currentPage, int pageSize, String username) {
            IPage<Favoriteitem> favoriteitemPage = new Page<>(currentPage, pageSize);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);
            favoriteitemDao.selectPage(favoriteitemPage, queryWrapper);

            List<ItemVO> itemVOList = new ArrayList<>();
            for (Favoriteitem favoriteitem : favoriteitemPage.getRecords()) {
                  itemVOList.add(entityToVO(itemDao.selectById(favoriteitem.getItemid())));
            }

            IPage<ItemVO> itemVOPage = new Page<>(currentPage, pageSize);
            itemVOPage.setTotal(favoriteitemPage.getTotal());
            itemVOPage.setRecords(itemVOList);

            return CommonResponse.createForSuccess(itemVOPage);
      }

      private ItemVO entityToVO(Item item) {
            Picture picture = pictureDao.selectById(item.getPictureid());
            ItemVO itemVO = new ItemVO();
            itemVO.setId(item.getId());
            itemVO.setPictureid(item.getPictureid());
            itemVO.setShopid(item.getShopid());
            itemVO.setPrice(item.getPrice());
            itemVO.setStock(item.getStock());
            itemVO.setPictureid(item.getPictureid());
            itemVO.setDescription(item.getDescription());
            itemVO.setVideoid(item.getVideoid());
            itemVO.setAvailable(item.getAvailable());
            itemVO.setSrc(picture.getSrc());
            return itemVO;
      }

      private int getMaxItemevalId() {
            List<Integer> list = new ArrayList<>();
            for (Itemeval itemeval : itemevalDao.selectList(null)) {
                  list.add(itemeval.getId());
            }
            return Collections.max(list);
      }
}
